from datetime import datetime
from sqlalchemy import String, Integer, Column, orm
from app.models.base import Base


class Book(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(50), nullable=False)
    author = Column(String(30), default='未名')
    binding = Column(String(20))
    publisher = Column(String(50))
    price = Column(String(20))
    pages = Column(Integer)
    pubdate = Column(String(20))
    isbn = Column(String(15), nullable=False, unique=True)
    summary = Column(String(1000))
    image = Column(String(50))

    @orm.reconstructor # TODO 如果对象要保存时,会进入当前__init__方法,覆盖父类的__init__, curennt_time需要手动赋值
    # 当book对象不是实例化创建的时候,当前__init__方法不会进来,所以需要这样一个装饰器,让构造函数执行
    def __init__(self):
        self.fields = ['id', 'title', 'author', 'binding', 'publisher', 'price',
         'pages', 'pubdate', 'isbn', 'summary', 'image']